package com.duoketech.utils.pay.weixin.config;

import com.duoketech.config.Constant;
import com.duoketech.utils.pay.weixin.required.IWXPayDomain;
import com.duoketech.utils.pay.weixin.required.WXPayDomainSimpleImpl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;


public class WXPayConfigImpl extends WXPayConfig {

    private byte[] certData;
    private static WXPayConfigImpl INSTANCE;

    private String appID;
    private String mchID;
    private String spbillCreateIp;
    private String notifyUrl;
    private String key;

    @Override
	public String getAppID() {
		return Constant.WX_APPID;
	}

    @Override
	public void setAppID(String appID) {
		this.appID = appID;
	}

    @Override
	public String getMchID() {
		return Constant.WX_MCH_ID;
	}

    @Override
	public void setMchID(String mchID) {
		this.mchID = mchID;
	}

    @Override
	public String getSpbillCreateIp() {
		return "117.61.195.4";
	}

    @Override
	public void setSpbillCreateIp(String spbillCreateIp) {
		this.spbillCreateIp = spbillCreateIp;
	}

    @Override
	public String getNotifyUrl() {

        return Constant.WX_NOTIFY_DOMAIN;
//        return WXConstent.weixin_GZH.getNotify_domain()+"/wxpay/doPayCallBack";
	}

    @Override
	public void setNotifyUrl(String notifyUrl) {
		this.notifyUrl = notifyUrl;
	}

    @Override
	public String getKey() {
		return Constant.WX_PARTNER_KEY;
	}

    @Override
	public void setKey(String key) {
		this.key = key;
	}

	/**
     * 假如需要证书，请将证书路径配置好
     * @throws Exception
     */
    public WXPayConfigImpl() throws Exception{
//        String certPath = "D://CERT/common/apiclient_cert.p12";
//        File file = new File(certPath);
//        InputStream certStream = new FileInputStream(file);
//        this.certData = new byte[(int) file.length()];
//        certStream.read(this.certData);
//        certStream.close();
    }

    public static WXPayConfigImpl getInstance() throws Exception{
        if (INSTANCE == null) {
            synchronized (WXPayConfigImpl.class) {
                if (INSTANCE == null) {
                    INSTANCE = new WXPayConfigImpl();
                }
            }
        }
        return INSTANCE;
    }


    public InputStream getCertStream() {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }


    public int getHttpConnectTimeoutMs() {
        return 2000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }

    public IWXPayDomain getWXPayDomain() {
        return WXPayDomainSimpleImpl.instance();
    }

    public String getPrimaryDomain() {
        return "api.mch.weixin.qq.com";
    }

    public String getAlternateDomain() {
        return "api2.mch.weixin.qq.com";
    }

    @Override
    public int getReportWorkerNum() {
        return 1;
    }

    @Override
    public int getReportBatchSize() {
        return 2;
    }

}
